context: skip the Done receive in Err when the channel is closedchan - #80915
context: skip the Done receive in Err when the channel is closedchan#80915davidteather wants to merge 1 commit into
Conversation
|
This PR (HEAD: f2884a2) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/go/+/815961. Important tips:
|
|
Message from Ian Lance Taylor: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/815961. |
Err receives from the Done channel so that a non-nil error implies the
channel is closed. A blocking receive takes hchan.lock even when the
channel is already closed, and Done is lazy. Until something calls it,
cancel stores the package-level closedchan. Every canceled context in
the program shares one lock.
BenchmarkErrCanceledParallelDistinct gives each goroutine its own
context and is just as slow as the shared-context case, which is what
identifies the shared global rather than any per-context state.
closedchan is closed at init and cannot reopen, so observing it needs
no synchronization. Skip the receive in that case. Done still supplies
the channel, so a cancel in flight blocks on c.mu as before.
goos: darwin
goarch: arm64
pkg: context
cpu: Apple M2 Pro
│ old │ new │
│ sec/op │ sec/op vs base │
ErrOK-10 2.017n ± 2% 1.979n ± 2% ~ (p=0.427 n=25)
ErrCanceled-10 14.000n ± 2% 3.973n ± 2% -71.62% (p=0.000 n=25)
ErrOKParallel-10 0.3072n ± 7% 0.3191n ± 3% ~ (p=0.184 n=25)
ErrCanceledParallel-10 36.1900n ± 1% 0.7030n ± 8% -98.06% (p=0.000 n=25)
ErrCanceledParallelDistinct-10 36.8500n ± 2% 0.6554n ± 9% -98.22% (p=0.000 n=25)
geomean 6.496n 1.029n -84.15%
B/op and allocs/op are unchanged. ErrOK and ErrOKParallel return before
the changed branch.
Fixes golang#80914
f2884a2 to
86eaaf2
Compare
|
This PR (HEAD: 86eaaf2) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/go/+/815961. Important tips:
|
|
Message from David Teather: Patch Set 2: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/815961. |
|
Message from Ian Lance Taylor: Patch Set 2: Commit-Queue+1 Please don’t reply on this GitHub thread. Visit golang.org/cl/815961. |
|
Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com: Patch Set 2: Dry run: CV is trying the patch. Bot data: {"action":"start","triggered_at":"2026-08-18T00:18:48Z","revision":"3ba348a463a306afc069c4b2d024136c6f4695da"} Please don’t reply on this GitHub thread. Visit golang.org/cl/815961. |
|
Message from Ian Lance Taylor: Patch Set 2: -Commit-Queue (Performed by <GERRIT_ACCOUNT_60063> on behalf of <GERRIT_ACCOUNT_5206>) Please don’t reply on this GitHub thread. Visit golang.org/cl/815961. |
|
Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com: Patch Set 2: This CL has passed the run Please don’t reply on this GitHub thread. Visit golang.org/cl/815961. |
|
Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com: Patch Set 2: LUCI-TryBot-Result+1 Please don’t reply on this GitHub thread. Visit golang.org/cl/815961. |
|
Message from Ian Lance Taylor: Patch Set 2: Auto-Submit+1 Code-Review+2 (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/815961. |
Err receives from the Done channel so that a non-nil error implies the
channel is closed. A blocking receive takes hchan.lock even when the
channel is already closed, and Done is lazy. Until something calls it,
cancel stores the package-level closedchan. Every canceled context in
the program shares one lock.
BenchmarkErrCanceledParallelDistinct gives each goroutine its own
context and is just as slow as the shared-context case, which is what
identifies the shared global rather than any per-context state.
closedchan is closed at init and cannot reopen, so observing it needs
no synchronization. Skip the receive in that case. Done still supplies
the channel, so a cancel in flight blocks on c.mu as before.
goos: darwin
goarch: arm64
pkg: context
cpu: Apple M2 Pro
│ old │ new │
│ sec/op │ sec/op vs base │
ErrOK-10 2.017n ± 2% 1.979n ± 2% ~ (p=0.427 n=25)
ErrCanceled-10 14.000n ± 2% 3.973n ± 2% -71.62% (p=0.000 n=25)
ErrOKParallel-10 0.3072n ± 7% 0.3191n ± 3% ~ (p=0.184 n=25)
ErrCanceledParallel-10 36.1900n ± 1% 0.7030n ± 8% -98.06% (p=0.000 n=25)
ErrCanceledParallelDistinct-10 36.8500n ± 2% 0.6554n ± 9% -98.22% (p=0.000 n=25)
geomean 6.496n 1.029n -84.15%
B/op and allocs/op are unchanged. ErrOK and ErrOKParallel return before
the changed branch.
Fixes #80914